package com.skyworth.ui.customview;

import android.animation.Animator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.OvershootInterpolator;
import android.widget.LinearLayout;
import android.widget.ScrollView;

import java.util.HashMap;

/**
 * Created by luwei on 15-12-15.
 */
public class ExpandMenuView extends ScrollView implements BaseAdapter.ObserverListener{

    private ExpandAdapter adapter = null;
    private LinearLayout mainLayout = null;
    private Context context = null;
    private HashMap<View,View> subMenuCache = new HashMap<View, View>();
    private HashMap<View,Integer> heightCache = new HashMap<View, Integer>();
    private HashMap<View,View> oneSubMenuCache = new HashMap<View, View>();
    private OnMenuClickListener menuClickListener = null;
    private int subMarginLeft = 0;
    private boolean expand = true;

    /**
     * 监听菜单点击接口
     */
    public interface OnMenuClickListener{
        /**
         * 一级菜单点击回调
         * @param pos 点击位置
         */
        public void oneMenuClick(int pos);

        /**
         * 二级菜单点击回调
         * @param onePos 一级菜单位置
         * @param subPos 二级菜单位置
         */
        public void subMenuClick(int onePos,int subPos);
    }

    public ExpandMenuView(Context context) {
        super(context);
        init(context);
    }

    public ExpandMenuView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public ExpandMenuView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context);
    }

    /**
     * 注册监听菜单点击事件接口
     * @param listener
     */
    public void setOneMenuClickListener(OnMenuClickListener listener){
        this.menuClickListener = listener;
    }

    /**
     * 设置是否支持展开收缩模式，如果不支持则直接以展开形式显示
     * @param expand false为不支持，true为支持
     */
    public void supportExpand(boolean expand){
        this.expand = expand;
    }

    /**
     * 设置二级菜单相对一级菜单的左侧距离
     * @param marginLeft
     */
    public void setSubMarginLeft(int marginLeft){
        this.subMarginLeft = marginLeft;
    }

    private void init(Context context){
        this.context = context;
        mainLayout = new LinearLayout(context);
        mainLayout.setOrientation(LinearLayout.VERTICAL);
        LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
        addView(mainLayout,params);
    }

    public void setAdapter(ExpandAdapter adapter){
        this.adapter = adapter;
        this.adapter.registObserver(this);
    }

    @Override
    public void onChanaged() {
        int count = adapter.getCount();
        for (int i = 0; i < count; i++){
            View view = adapter.getOneView(i,null,mainLayout);
            view.setOnClickListener(oneMenuClickListener);
            view.setId(i);
            mainLayout.addView(view);
            int subCount = adapter.getSubCount(i);
            if (subCount > 0){
                LinearLayout subLayout = new LinearLayout(context);
                subLayout.setOrientation(LinearLayout.VERTICAL);
                int height = 0;
                for(int j = 0; j < subCount; j++){
                    View subView = adapter.getView(j,null,subLayout);
                    oneSubMenuCache.put(subView,view);
                    subView.setId(j);
                    ViewGroup.LayoutParams params = subView.getLayoutParams();
                    height = params.height + height;
                    subView.setOnClickListener(subMenuClickListener);
                    subLayout.addView(subView);
                }
                int subHeight = 0;
                if (!expand){
                    subHeight = LinearLayout.LayoutParams.WRAP_CONTENT;
                }else{
                    enableSubMenuFocus(false,subLayout);
                }
                LinearLayout.LayoutParams subParams = new LinearLayout.LayoutParams(
                        ViewGroup.LayoutParams.WRAP_CONTENT, subHeight);
                subLayout.setPadding(subMarginLeft,0,0,0);
                mainLayout.addView(subLayout,subParams);
                subMenuCache.put(view,subLayout);
                heightCache.put(subLayout,height);
            }

        }
    }

    OnClickListener oneMenuClickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (expand){
                final View view = subMenuCache.get(v);
                if (view != null){
                    final ViewGroup.LayoutParams params = view.getLayoutParams();
                    if(params.height == 0){
                        expandByAnimation((ViewGroup) view, params,0,heightCache.get(view));
                    }else{
                        expandByAnimation((ViewGroup) view, params,heightCache.get(view),0);
                    }
                }
            }
            if (menuClickListener != null)
                menuClickListener.oneMenuClick(v.getId());
        }
    };

    private void expandByAnimation(final ViewGroup view, final ViewGroup.LayoutParams params,final int start,final int end) {
        ValueAnimator animator = ValueAnimator.ofFloat(start,end);
        if (start < end){
            animator.setInterpolator(new OvershootInterpolator());
        }
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                params.height = ((Float)animation.getAnimatedValue()).intValue();
                view.setLayoutParams(params);
            }
        });
        animator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {
                if (start > end){
                    enableSubMenuFocus(false,view);
                }
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                if (start < end){
                    enableSubMenuFocus(true,view);
                }
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
        animator.setDuration(200);
        animator.start();
    }

    private void enableSubMenuFocus(boolean focus,ViewGroup subLayout){
        int count = subLayout.getChildCount();
        for(int i = 0; i < count; i++){
            subLayout.getChildAt(i).setFocusable(focus);
            subLayout.getChildAt(i).setFocusableInTouchMode(focus);
        }
    }

    OnClickListener subMenuClickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (menuClickListener != null)
                menuClickListener.subMenuClick(oneSubMenuCache.get(v).getId(),v.getId());
        }
    };
}
